//自定义hooks，获取鼠标位置
import { useEffect, useState } from "react";



const useMouse = ()=>{
  const [x, setX] = useState(0)
  const [y, setY] = useState(0)

  const mouseMoveHandler = (event: MouseEvent) => {
    setX(event.clientX)
    setY(event.clientY)
  }
  
  useEffect(() => {
    //监听鼠标事件
    window.addEventListener('mousemove', mouseMoveHandler)

    //组件销毁后,移除鼠标事件，避免内存泄漏
    return () => {
      window.removeEventListener('mousemove', mouseMoveHandler)
    }
  })

  return {x, y}
}

export default useMouse